﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.Domains;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Unity;
using Microsoft.Practices.Unity;

namespace Core.DAO
{
    public class BuildingDAO : AbstractDAO<IBuilding>, IBuildingDAO
    {
        public BuildingDAO(IConnection connection) : base(connection)
        {
        }


        protected override void GetDependendModels(object domainobject, object[] row)
        {
            var building = (IBuilding) domainobject;
            var districtDao = Unity.ResolverContainer.Resolver.Resolve<IDistrictDAO>();
            building.District = districtDao.GetAllByField("district_id", row[4]).First();
        }

        public List<IBuilding> GetAllBuildingsByAction(IAction action)
        {
            var buildings = GetAll(false);
            var idis = Connection.ExecuteSelectQuery("SELECT register_id FROM building_action WHERE id_action="+action.ActionId,1);
            var pre = buildings.Where(x => idis.Count(y => y.ToString() == x.RegisterID) > 0);
            return buildings.Where(x => idis.Count(y => y[0].ToString() == x.RegisterID) > 0).ToList();
        }
    }
}
